package com.atguigu.srb.base.algorithm;

/**
 * @ClassName Solution9
 * @Describe
 * @Author Wangchengshan
 * @Date 2023/6/21 14:54
 * @Version 1.0
 **/

//跳跃游戏3
//
//给你一个下标从 0 开始的二进制字符串 s 和两个整数 minJump 和 maxJump 。一开始，你在下标 0 处，且该位置的值一定为 '0' 。当同时满足如下条件时，你可以从下标 i 移动到下标 j 处：
//
//i + minJump <= j <= min(i + maxJump, s.length - 1) 且
//s[j] == '0'.
//如果你可以到达 s 的下标 s.length - 1 处，请你返回 true ，否则返回 false 。
//
//输入：s = "011010", minJump = 2, maxJump = 3
//输出：true
//
//输入：s = "01101110", minJump = 2, maxJump = 3
//输出：false
public class Solution9 {
    public static boolean canReach(String s, int minJump, int maxJump) {
        //min是0
        int index = s.length() - 1;
        int le = s.length();
        int left = minJump;
        boolean returnResult = true;
        int right = maxJump;
        while (le >= minJump) {
            if(le == minJump){
                break;
            }
            int min = Math.min(maxJump, index);
            if(min>index){
                min = index;
            }else {
                if (s.charAt(min) != '0' ) {
                    int rights = right;
                    int mins = min;
                    while (left <= rights) {
                        if (s.charAt(mins--)!= '0') {
                            rights --;
                            returnResult = false;
                        }else {
                            returnResult = true;
                            s = s.substring(min, index + 1);
                            index = s.length()-1;
                            le = s.length();
                            break;
                        }
                    }
                }else {
                    s = s.substring(min, index + 1);
                    index = s.length()-1;
                    le = s.length();
                }
                if (!returnResult && le <minJump) {
                    break;
                }
            }
        }
        return s.length() == 1 ? true : false;
    }

    public static void main(String[] args) {
        String s1 = "011010";
        String s2 = "01101110";
        String s3 = "01111110";
        String s4 = "00";
        String s5 = "000100100010";
        boolean b = canReach(s5, 1, 2);
        System.out.println(b);
    }
}
